package ccm.component.image;

import ccm.component.override.JLabel;
import ccm.component.override.JSpinner;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class ManualResizeableImageJPanel extends AutoResizeableImageJPanel implements ChangeListener, KeyListener
{
    protected JSpinner resizeJSpinner;
    protected JPanel infoJPanel;


    protected int[] preventKeyCode;

    public ManualResizeableImageJPanel()
    {
        this(null);
    }

    public ManualResizeableImageJPanel(BufferedImage image)
    {
        super(image);

        setLayout(new BorderLayout());
        JPanel imgJPanel=new JPanel();
        add(imgJPanel);
        imgJPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        imgJPanel.add(imgJLabel,BorderLayout.CENTER);
        imgJPanel.addComponentListener(this);
        resizeRate=1;

        infoJPanel=new JPanel();
        add(infoJPanel,BorderLayout.NORTH);
        infoJPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel,BorderLayout.NORTH);
            jPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            jPanel.add(new JLabel("缩放:"));
            resizeJSpinner=new JSpinner(new SpinnerNumberModel(1.0f,0.1f,100.0f,0.1f));
            jPanel.add(resizeJSpinner);
            resizeJSpinner.addChangeListener(this);
            ((JSpinner.DefaultEditor)resizeJSpinner.getEditor()).getTextField().addKeyListener(this);
        }

        this.preventKeyCode=new int[]{
                KeyEvent.VK_Q,
                KeyEvent.VK_W,
                KeyEvent.VK_E,
                KeyEvent.VK_E,
                KeyEvent.VK_R,
                KeyEvent.VK_T,
                KeyEvent.VK_Y,
                KeyEvent.VK_U,
                KeyEvent.VK_I,
                KeyEvent.VK_O,
                KeyEvent.VK_P,
                KeyEvent.VK_A,
                KeyEvent.VK_S,
                KeyEvent.VK_D,
                KeyEvent.VK_F,
                KeyEvent.VK_G,
                KeyEvent.VK_H,
                KeyEvent.VK_J,
                KeyEvent.VK_K,
                KeyEvent.VK_L,
                KeyEvent.VK_Z,
                KeyEvent.VK_X,
                KeyEvent.VK_C,
                KeyEvent.VK_V,
                KeyEvent.VK_B,
                KeyEvent.VK_N,
                KeyEvent.VK_M,
        };

        updateImg();
    }

    public void setPreventKeyCode(int[] preventKeyCode)
    {
        this.preventKeyCode=preventKeyCode;
    }

    /**
     * 在手动设置缩放的输入框中值发生变化时自动更新缩放
     */
    public void stateChanged(ChangeEvent e)
    {
        if(e.getSource().equals(resizeJSpinner))
        {setResizeRate(((Number)(resizeJSpinner.getValue())).floatValue());}
    }

    public void addKeyListener(KeyListener keyListener)
    {
        resizeJSpinner.addKeyListener(keyListener);
        ((JSpinner.DefaultEditor)resizeJSpinner.getEditor()).getTextField().addKeyListener(keyListener);
    }

    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        ((JSpinner.DefaultEditor)(resizeJSpinner).getEditor()).getTextField().setEditable(!(isPreventKey(e.getKeyCode())));
    }

    protected boolean isPreventKey(int key)
    {
        if(preventKeyCode==null)
        {return false;}
        for(int k: preventKeyCode)
        {
            if(k==key)
            {return true;}
        }
        return false;
    }

    @Override
    public void keyReleased(KeyEvent e)
    {

    }
}
